package com.party.mobile.web.controller.member;

import com.party.authorization.annotation.Authorization;
import com.party.common.paging.Page;
import com.party.core.model.YesNoStatus;
import com.party.core.model.sign.MemberSign;
import com.party.core.model.sign.MemberSignDetail;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.sign.IMemberSignDetailService;
import com.party.core.service.sign.IMemberSignService;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.biz.member.MemberSignDetailBizService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.sign.output.SignRecodListOutput;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: huangzequn
 * @Date: 2019/4/16 10:36
 * 合作商签到控制器
 */
@Controller
@RequestMapping(value = "institutions/app/sign")
public class InstitutionsAppSignController {

    protected static Logger logger = LoggerFactory.getLogger(InstitutionsAppSignController.class);

    public static MemberSign globalMemberSign = new MemberSign();

    @Autowired
    private CurrentUserBizService currentUserBizService;

    @Autowired
    private IMemberSignService memberSignService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private MemberSignDetailBizService memberSignDetailBizService;

    @Autowired
    private IMemberSignDetailService memberSignDetailService;

    /**
    * @Author hzq
    * @Description //进入app自动签到接口
    * @Date 14:47 2019/4/16
    * @param request
    * @return
    **/
    @ResponseBody
    @Authorization
    @RequestMapping(value = "save")
    public AjaxResult save(HttpServletRequest request) {
        Map<String,Object> map = new HashMap();
        try {
            String memberId = currentUserBizService.getCurrentUser(request).getId();
            MemberSign memberSign = memberSignService.getSet(memberId);
            if (DateUtils.isSameDay(memberSign.getLastSignTime(), new Date())) {
                //如果这个机构最后签到时间和当前时间是同一天的话，就说明今天已经签过到了
                map.put("isWindow", YesNoStatus.NO.getCode());
                map.put("signTotal",memberSign.getSignTotal());
                map.put("continuousTotal",memberSign.getContinuousTotal());
                return AjaxResult.success(map);
            }
            memberSignService.update(memberSign);
            map.put("isWindow", YesNoStatus.YES.getCode());
            map.put("signTotal",memberSign.getSignTotal());
            map.put("continuousTotal",memberSign.getContinuousTotal());
            return AjaxResult.success(map);
        } catch (Exception e) {
            logger.error("自动签到异常", e);
            return AjaxResult.error(100, "自动签到异常");
        }
    }

    /**
    * @Author hzq
    * @Description //签到日历接口
    * @Date 16:57 2019/4/16
    * @param request
    * @param yearMonth  年月 '2019-04'
    * @return
    **/
    @ResponseBody
    @Authorization
    @RequestMapping(value = "calendar")
    public AjaxResult calendar(HttpServletRequest request,String yearMonth) {
        try {
            String memberId = currentUserBizService.getCurrentUser(request).getId();
            MemberSign memberSign = memberSignService.getSet(memberId);
            List<SignRecodListOutput> calendarRecordList = memberSignDetailBizService.myCalendarSignList(memberSign.getId(), yearMonth);
            return AjaxResult.success(calendarRecordList);
        } catch (Exception e) {
            logger.error("获取我的签到日历及记录异常", e);
            return AjaxResult.error();
        }
    }

    /**
    * @Author hzq
    * @Description //签到详情
    * @Date 15:38 2019/4/17
    * @param request
    * @param yearMonth
    * @return
    **/
    @ResponseBody
    @Authorization
    @RequestMapping(value = "detail")
    public AjaxResult detail(HttpServletRequest request, String yearMonth, Page page) {
        try {
            String memberId = currentUserBizService.getCurrentUser(request).getId();
            MemberSign memberSign = memberSignService.getSet(memberId);
            List<MemberSignDetail> calendarRecordList = memberSignDetailService.getByYearMonth(memberSign.getId(), yearMonth,page);
            return AjaxResult.success(calendarRecordList, page);
        } catch (Exception e) {
            logger.error("获取我的签到日历及记录异常", e);
            return AjaxResult.error();
        }
    }

    /**
    * @Author hzq
    * @Description //初始化 Member_sign
    * @Date 15:05 2019/4/16
    * @param
    * @return
    **/
    @ResponseBody
    @RequestMapping(value = "initializeMemberSign")
    public AjaxResult initializeMemberSign() {
        try {
            List<String> memberList = memberGroupService.getAllPartnerIds();
            for (String member : memberList) {
                 MemberSign memberSign = memberSignService.getByMemberId(member);
                if(memberSign == null){
                    globalMemberSign.setMemberId(member);
                    memberSignService.insert(globalMemberSign);
                }
            }
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("初始化 Member_sign异常", e);
            return AjaxResult.error(100, "初始化 Member_sign异常");
        }
    }


}
